<?php
//+--------------------------------------------
//|描述
//|--------------------------------------------
//|Created by PhpStorm.
//| Author: DRG
//+-------------------------------------------
//|Date: 2018/5/30
//| Time: 13:57
//+--------------------------------------------

echo 'start ' . date('H:i:s').PHP_EOL;
$urls = [
    'http://baidu.com',
    'http://qq.com',
    'http://sina.com',
    'http://tencent.com',
    'http://www.e-cq.cn',
];

for ($i = 0; $i < 5; $i++){
    //子进程
    $process = new swoole_process(function (swoole_process $worker) use($i,$urls){
        //curl
        $content = curlData($urls[$i]);
        //echo $content.PHP_EOL;
        //或者通过    $worker->weite  写入管道 $data的长度在Linux系统下最大不超过8K，MacOS/FreeBSD下最大不超过2K
        $worker->write($content.PHP_EOL);
    },true);
    
    $pid = $process->start();//开启一个子进程
    $workers[$pid] = $process;
}

foreach ($workers as $k=>$process) {
    echo $process->read();
}
/**
 * 模拟耗时请求url内容  1S
 * @param $url
 * @return string
 * @author DRG
 */
function curlData($url) {
    //curl  file_get_contents
    sleep(1);
    return $url ."success".PHP_EOL;
}

echo 'process-end-time ' . date('H:i:s').PHP_EOL;